package com.example.chinawenddingexpo.controller;

import com.example.chinawenddingexpo.model.Coupon;
import com.example.chinawenddingexpo.model.UserCoupon;
import com.example.chinawenddingexpo.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/coupon")
public class CouponController {
    
    @Autowired
    private CouponService couponService;
    
    // 获取所有可用优惠券
    @GetMapping("/available")
    public Map<String, Object> getAvailableCoupons() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Coupon> coupons = couponService.getAllAvailableCoupons();
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", coupons);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取优惠券列表失败");
        }
        return result;
    }
    
    // 获取指定参展商的优惠券
    @GetMapping("/exhibitor/{exhibitorId}")
    public Map<String, Object> getCouponsByExhibitor(@PathVariable Long exhibitorId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Coupon> coupons = couponService.getAvailableCouponsByExhibitor(exhibitorId);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", coupons);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取参展商优惠券失败");
        }
        return result;
    }
    
    // 领取优惠券
    @PostMapping("/receive")
    public Map<String, Object> receiveCoupon(@RequestBody Map<String, Long> receiveData) {
        Map<String, Object> result = new HashMap<>();
        try {
            Long userId = receiveData.get("userId");
            Long couponId = receiveData.get("couponId");
            
            boolean success = couponService.receiveCoupon(userId, couponId);
            if (success) {
                result.put("code", 200);
                result.put("msg", "领取成功");
            } else {
                result.put("code", 400);
                result.put("msg", "领取失败，优惠券已领完或已过期");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "领取优惠券失败");
        }
        return result;
    }
    
    // 获取用户的优惠券
    @GetMapping("/user/{userId}")
    public Map<String, Object> getUserCoupons(@PathVariable Long userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<UserCoupon> coupons = couponService.getUserCoupons(userId);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", coupons);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取用户优惠券失败");
        }
        return result;
    }
    
    // 获取用户未使用的优惠券
    @GetMapping("/user/{userId}/unused")
    public Map<String, Object> getUserUnusedCoupons(@PathVariable Long userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<UserCoupon> coupons = couponService.getUserUnusedCoupons(userId);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", coupons);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取未使用优惠券失败");
        }
        return result;
    }
    
    // 使用优惠券
    @PostMapping("/use")
    public Map<String, Object> useCoupon(@RequestBody Map<String, String> useData) {
        Map<String, Object> result = new HashMap<>();
        try {
            Long userId = Long.valueOf(useData.get("userId"));
            Long couponId = Long.valueOf(useData.get("couponId"));
            String orderNo = useData.get("orderNo");
            
            boolean success = couponService.useCoupon(userId, couponId, orderNo);
            if (success) {
                result.put("code", 200);
                result.put("msg", "使用成功");
            } else {
                result.put("code", 400);
                result.put("msg", "使用失败，优惠券不存在或已使用");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "使用优惠券失败");
        }
        return result;
    }
    
    // 创建优惠券
    @PostMapping
    public Map<String, Object> createCoupon(@RequestBody Coupon coupon) {
        Map<String, Object> result = new HashMap<>();
        try {
            Coupon savedCoupon = couponService.saveCoupon(coupon);
            result.put("code", 200);
            result.put("msg", "创建成功");
            result.put("data", savedCoupon);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "创建优惠券失败");
        }
        return result;
    }
}